var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		datalist: [],
		pagenum: 1,
		id:0,
	},
	_onLoad: function () {
		var that = this
		var id = that.options.id
		if (that.options && that.options.scene) {
			var scene = that.options.scene
			var scenes = scene.split('_');
			if(scenes[1]){
				id = parseInt(scenes[1]);
			}
		}
		$.get('article/detail', { id: id}, function (res) {
			if (res.status == 1) {
				res.sysset = res._sysset
				res.sysset.textset = that.data.textset
				that.setData(res);
				var pagecontent = JSON.parse(res.detail.content);
				var _bannerindex = [];
				for(var i in pagecontent){
					_bannerindex[i] = 0;
				}
				that.setData({ pagecontent:pagecontent,_bannerindex:_bannerindex,title:res.detail.name,sharepic:res.detail.pic})
				wx.setNavigationBarTitle({ title: res.detail.name})
			} else {
				$.alert(res.msg);
			}
		})
	},
	getDataList: function () {
		var that = this
		var pagenum = that.data.pagenum
    $.post('article/detail', { pagenum: pagenum, id:that.data.detail.id }, function (res) {
      var data = res.data
			if (pagenum == 1) {
				that.setData({ datalist: data });
				if (data.length == 0) {
					$.showEmpty('暂无相关数据！');
				}
			} else {
				if (data.length == 0) {
					that.setData({ $nomoreData: true });
				} else {
					var datalist = that.data.datalist
					var newdata = datalist.concat(data)
					that.setData({ datalist: newdata });
				}
			}
		});
	},
	onReachBottom: function () {
		if (!this.data.$dataEmpty.visible && !this.data.$nomoreData) {
			this.setData({ pagenum: this.data.pagenum + 1 })
			this.getDataList();
		}
	},
	zan:function(e){
		var that = this
		var id = e.currentTarget.dataset.id
		$.post("article/zan",{id:id},function(res){
			if(res.type==0){ //取消点赞
				var iszan = 0;
			}else{
				var iszan = 1;
			}
			that.setData({iszan:iszan,'detail.zan':res.zancount});
		})
	},
	pzan:function(e){
		var that = this
		var id = e.currentTarget.dataset.id
		var index = e.currentTarget.dataset.index
		var datalist = that.data.datalist
		$.post("article/pzan",{id:id},function(res){
			if(res.type==0){ //取消点赞
				var iszan = 0;
			}else{
				var iszan = 1;
			}
			datalist[index].iszan = iszan
			datalist[index].zan = res.zancount
			that.setData({datalist:datalist});
		})
	},
  showpinglun:function(){
      
  }
}));